/*
 * main.cpp
 *
 * This file is a part of TermOSEmulator.
 *
 * Written by Peter Winchester <wincsoft_pw@outlook.com>
 * Copyright (C) 2021 Peter Winchester.
 *
 * TermOSEmulator is free software, follows the GNU General
 * Public License.
 *
 * NOTE: TermOSEmulator requires Windows API, so it can only
 * run on the Windows.
 *
 */

#include "TermOSEmulator.h"

char cmdTyped[MAX_STRING_LEN];
string command;
vector<string> cmdArgs;

int initTermOSEmulator();
void startUp();
int getSystemRootPath();
int initApplications();
int TranslateCmd();
int ProcessCmd();

int main(int argc, char const *argv[])
{
    if (!initTermOSEmulator()) //初始化TOSE失败
    {
        printf("Error! Cannot load system data.\n");
        printf("Please check the files in the /dat directory.\n");
        cmdArgs.clear();
        pause(cmdArgs);
        return 0;
    }
    startUp();

    /* 这是整个用户的操作过程 */
    while (toseRunning)
    {
        /* 打印当前目录 */
        for (int i = 0; i < pathCrt.size(); i++)
        {
            cout << pathCrt[i];
            if (i < pathCrt.size() - 1)
                printf("/");
        }
        printf(" $ ");
        gets(cmdTyped);     //输入命令
        if (TranslateCmd()) //翻译输入的命令
        {
            ProcessCmd();   //对命令进行处理
        }
    }
    return 0;
}

//初始化TOSE
int initTermOSEmulator()
{
    toseRunning = true; //TOSE开始运行
    color(7);
    if (!getSystemRootPath())
        return 0; //获取系统根目录失败
    if (!initExplorer())
        return 0; //初始化资源管理器失败
    if (!initApplications())
        return 0; //初始化应用程序失败
    return 1;
}

//加载开始界面
void startUp()
{
    printf("Welcome to TermOSEmulator!\n\n");
    printf("Copyright (C) 2021 Peter Winchester.\n\n");
    printf("TermOSEmulator is free software, follows the GNU General Public License.\n");
    printf("See the file LICENSE for more information about GPL.\n");
    printf("Visit https://github.com/PeterWinchester/TermOSEmulator for source code.\n");
    printf("The writer's email is wincsoft_pw@outlook.com. To report bugs, please\n");
    printf("visit https://github.com/PeterWinchester/Termosemulator/issues, or you\n");
    printf("can mail the writer.\n\n");
    printf("Type 'help' for help or type 'exit' to quit.\n\n");
}

//从 /dat/systempath.txt 中获取系统根目录
int getSystemRootPath()
{
    FILE *fin = fopen("../dat/rootpath.txt", "r");
    if (fin == NULL)
        return 0; //找不到文件，失败
    int len = 0;
    char cGet;
    while (fscanf(fin, "%c", &cGet) == 1 && cGet != '\r')
        systemRootPath[len++] = cGet;
    fclose(fin);
    return 1;
}

//初始化应用程序
int initApplications()
{
    /* 检查文件 */
    FILE *test = fopen("../dat/sysappdat.txt", "r");
    if (test == NULL) //找不到文件
    {
        return 0;
    }

    /* 获取应用数据。 */
    ifstream fin("../dat/sysappdat.txt");
    fin >> numApplications;
    for (int i = 0; i < numApplications; i++)
    {
        fin >> apps[i].name;
    }
    fin.close();
    return 1;
}

//翻译输入的命令，即把命令中的单词拆解开来，并确定命令和参数
int TranslateCmd()
{
    if (strlen(cmdTyped) == 0) //没输入任何东西
    {
        return 0;              //不需要翻译
    }

    /* 开始处理 */
    cmdArgs.clear();
    for (int i = 0; i < strlen(cmdTyped); i++)
    {
        if (cmdTyped[i] != ' ' && (i == 0 || cmdTyped[i - 1] == ' '))
        {
            /* 碰到一个新单词 */
            string tmp;
            tmp.push_back(cmdTyped[i]);
            cmdArgs.push_back(tmp); //添加到列表里
        }
        else if (cmdTyped[i] != ' ') //不是新单词，指当前单词的下一个字母
        {
            cmdArgs[cmdArgs.size() - 1].push_back(cmdTyped[i]); //添加一个字符
        }
    }
    if (cmdArgs.size() == 0)
        return 0;         //未输入任何命令
    command = cmdArgs[0]; //取列表中的第一个单词作为命令名称

    /* 将命令参数存储到参数列表中 */
    for (int i = 0; i < cmdArgs.size() - 1; i++)
    {
        cmdArgs[i] = cmdArgs[i + 1];
    }
    cmdArgs.pop_back();
    return 1; //完成翻译
}

//处理命令
int ProcessCmd()
{
    /* 在命令列表中查找该命令。若找到则执行该命令 */
    for (int i = 0; i < NUM_COMMANDS; i++)
    {
        if (cmdName[i] == command)
        {
            return cmdOpt[i](cmdArgs);
        }
    }

    /* 未找到输入的命令，则在应用程序中查找，看输入的是否应用程序名称 */
    for (int i = 0; i < numApplications; i++)
    {
        if (command == apps[i].name)
        {
            return startApplication(i); //启动该应用程序
        }
    }
    printf("'");
    cout << command;
    printf("' is not a command or an executable file.\n");
    return 0; //既不是应用程序也不是命令，返回0
}
